(function( $, window ) {
  var UploadManager = {

    allow_exts: [
        'gif','png','jpg','jpeg', //images
        'ppt', 'pptx', 'pps', 'ppsx', //ppt
        'xls', 'xlsx', //excel
        'doc', 'docx', //word
        'pdf', 'txt',
        'dmg','apk',//测试
    ],
    leftFileSize: 2,

    queue: [],


    fileSizePlus: function(){
      this.leftFileSize += 1;
    },

    fileSizeMinus: function(){
      this.leftFileSize -= 1;
    },

    onlyImages: function() {
      this.allow_exts = [
          'gif','png','jpg','jpeg', //images
      ];
    },

    newUploader: function() {
      // insert input
      var id = HMUtil.randId();
      $(document.body).append('<input class="hide" id="'+id+'" type="file" name="file" data-url="/upload" multiple>');
      setTimeout(function(){
        $('#' + id).fileupload({
            dataType: 'json',
            add: function(e, data) {
              var id = $(this).attr("id");
              var file = data.files[0];
              data.randId = HMUtil.randId();

              var ext = file.name.split('.').pop().toLowerCase();

              var allow_exts = UploadManager.allow_exts;

              if($.inArray(ext, allow_exts) == -1) {
                  alert('文件格式不支持');
                  UploadManager.unQueue(id);
                  return;
              } else {
                data.submit();
              }

            },

            done: function (e, data) {
              var id = $(this).attr("id");
              var params = UploadManager.queue[id];
              UploadManager.unQueue(id);
              var callback = params.uploadCallback || window.uploadCallback;
              if (callback) {
                callback(data.result, data.randId);
              }
            },

            change: function (e, data) {
              var limit = UploadManager.leftFileSize;
              if (data.files.length > limit) {
                alert("最多还能上传"+ limit +"份文件");
                return false;
              }
              return true;
            },

            progress: function (e, data) {
              console.log("progress", e, data);
              var id = $(this).attr("id");
              var params = UploadManager.queue[id];
              var callback = params.progressallCallback || window.progressallCallback;
              if (callback) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                callback(progress,
                    data.randId,
                    data.files[0].name,
                    id);
              }
            }
        });
      }, 500);
      return id;
    },

    unQueue: function(id) {
      var params = UploadManager.queue[id];
      params.uploading = false;
      UploadManager.queue[id] = params;
    },

    upload: function(params) {
      var id = this.pickOne();

      if (id) {
        params = $.extend(params, {uploading: true})
        this.queue[id] = params;
        $("#" + id).click();
      } else {
        alert("上传队列已满");
      }
      return id;

    },

    pickOne: function() {
      for( var id in this.queue ){
        var params = this.queue[id];
        if (!params.uploading) {
          return id;
        }
      }
      return null;
    },

    init: function() {
      var that = this;

      $.each([1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10], function(){
        var id = that.newUploader();
        that.queue[id] = {uploading: false};
      });
    },
  };
  window.UploadManager = UploadManager;
  UploadManager.init();
}( jQuery, window ));
